﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Data;

namespace Tarea3_Virginia_Opazo
{
    /// <summary>
    /// Controla los requerimientos a la base de datos
    /// </summary>
    class AccesoBD
    {
        public MySqlConnection Conexion
        {
            get { return conexion; }
        }
        private MySqlConnection conexion;
        private string connectionString;
        private MySqlCommand comando;
        private MySqlDataAdapter dataAdapter;
        
        
        public AccesoBD()
        {
            connectionString = "Server=localhost;Uid=root;Pwd=;Database=gestion_hh;";
            dataAdapter = new MySqlDataAdapter();
                        
        }

        public bool Conectar()
        {
            conexion = null;
            try
            {
                conexion = new MySqlConnection(connectionString);
                comando = conexion.CreateCommand();
                conexion.Open();
                conexion.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public int EjecutarInsertUpdateDelete(string consulta)
        {
            comando.CommandText = consulta;
            try
            {
                conexion.Open();
                int i=comando.ExecuteNonQuery();
                conexion.Close();
                return i;
            }
            catch
            {
                return -1;
            }
        }

        public DataTable EjecutarSelect(string consulta)
        {
            comando.CommandText = consulta;
            try
            {
                conexion.Open();
                dataAdapter.SelectCommand = comando;
                DataTable resultado = new DataTable();
                dataAdapter.Fill(resultado);
                conexion.Close();
                return resultado;
            }
            catch
            {
                return null;
            }
        }

        public List<string> EjecutarSelectL(string consulta)
        {
            comando.CommandText = consulta;
            List<string> res = new List<string>();
            try
            {
                conexion.Open();
                dataAdapter.SelectCommand = comando;
                DataTable resultado = new DataTable();
                dataAdapter.Fill(resultado);
                conexion.Close();
                foreach (DataRow row in resultado.Rows)
                {
                    res.Add(row[0].ToString());
                }
                return res;
            }
            catch
            {
                return null;
            }
        }

 

    }
}
